package com.qst.itoffer.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.qst.itoffer.bean.ApplicantBean;
import com.qst.itoffer.bean.ResumeBasicInfoBean;
import com.qst.itoffer.dao.ResumeDao;

/**
 * Servlet implementation class ResumeBasicInfoServlet
 */
@WebServlet("/ResumeBasicInfoServlet")
public class ResumeBasicInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ResumeBasicInfoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * 
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
		 }	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//设置请求和响应编码
				request.setCharacterEncoding("utf-8");
				response.setContentType("text/html;charset=utf-8");
				 ResumeDao resumeDao=new ResumeDao();
				//获取操作类型
				String type=request.getParameter("type");
				if("select".equals(type)) {
					//查询简历信息
					ResumeBasicInfoBean resumeInfo=new ResumeBasicInfoBean();
					if(request.getSession().getAttribute("ResumeId")!=null) {
						int resumeId=(Integer)request.getSession().getAttribute("ResumeId");
						resumeInfo=resumeDao.selectRsesumeInfoById(resumeId);
					}
					//将简历信息存入request对象属性中
					request.setAttribute("resumeInfo", resumeInfo);
					//使用请求转发到简历展示页面
					request.getRequestDispatcher("applicant/resume.jsp").forward(request, response);
				}else if("add".equals(type)) {
					//获取请求参数
                ResumeBasicInfoBean basicInfo=this.requestDataObj(request);
                //从session中获取当前登陆的求职者信息
                ApplicantBean applicantBean=(ApplicantBean)request.getSession().getAttribute("LoginApplicant");
                //为当前登陆的求职者添加简历信息
                int resumeId=resumeDao.addResume(basicInfo, applicantBean.getApplicant_id());
              //将简历ID存入session中
                request.getSession().setAttribute("ResumeId", resumeId);
            
                response.sendRedirect("ResumeBasicInfoServlet?type=select");
				}else if("updateSelect".equals(type)) {
					//查询简历信息
					ResumeBasicInfoBean resumeInfo=new ResumeBasicInfoBean();
					if(request.getSession().getAttribute("ResumeId")!=null) {
						int resumeId=(Integer)request.getSession().getAttribute("ResumeId");
						resumeInfo=resumeDao.selectRsesumeInfoById(resumeId);
					}
					//将简历信息存入request对象属性中
					request.setAttribute("resumeInfo", resumeInfo);
					//使用请求转发到简历展示页面
					request.getRequestDispatcher("applicant/resumeBasicInfoUpdate.jsp").forward(request, response);
				
				}else if("update".equals(type)) {
					//获取请求参数
	                ResumeBasicInfoBean basicInfo=this.requestDataObj(request);
	                //需要修改的简历标识id
	                int resumeId=(Integer)request.getSession().getAttribute("ResumeId");
	                System.out.println(resumeId);
	                resumeDao.updateResume(basicInfo,resumeId);
	                response.sendRedirect("ResumeBasicInfoServlet?type=select");
				}
				
		 }	
	  /**
	   * 统一处理请求数据，进行封装
	   * @param request
	   * @return
	   */
	   public ResumeBasicInfoBean requestDataObj(HttpServletRequest request) {
		  ResumeBasicInfoBean basicInfo=null;
		   String realName=request.getParameter("realName");
	        String gender=request.getParameter("gender");
	        String birthday=request.getParameter("birthday");
	        String current_loc=request.getParameter("current_loc");
	        String resident_loc=request.getParameter("resident_loc");
	        String telephone=request.getParameter("telephone");
	        String email=request.getParameter("email");
	        String jobIntension=request.getParameter("jobIntension");
	        String jobExperience=request.getParameter("jobExperience");
	        //将String转化为Date
	        Date birthdate=null;
	        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	         try {
				birthdate=sdf.parse(birthday);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        basicInfo=new ResumeBasicInfoBean(realName,gender,birthdate,current_loc,resident_loc,telephone,email,jobIntension,jobExperience, null);
	  		return basicInfo;
	}

}
